package com.example.demo6.controller;

import com.example.demo6.domain.Actor;
import com.example.demo6.domain.Movie;
import com.example.demo6.repository.MovieRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private MovieRepository movieRepository;

    @RequestMapping(value = "/add/{movieId}", method = RequestMethod.GET)
    public String toAdd(Model model,@PathVariable Long movieId){
        model.addAttribute("movieId", movieId);
        return "role/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String doAdd(@RequestParam Long movieId,@RequestParam String actorName, @RequestParam String roleName){

        //1.获取电影信息
        Movie movie = movieRepository.findOne(movieId);
        Actor actor = new Actor();
        actor.setName(actorName);
        movie.addRole(actor, roleName);
        movieRepository.save(movie);
        return "redirect:/movie/detail/" + movieId;
    }
}
